<?php
class Forum_Message
{
    /** 
     * $_namespace - namespace inside storage object
     * 
     * @var string
     */
    private static $_namespace = 'default';
    
    /**
     * @var Forum_Message
     */
    private static $_instance = null;
    
    /**
     * Retrieve Forum_Message object
     * 
     * @return Forum_Message
     */
    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    
    /**
     * Add message to current namespace
     * 
     * @param string $message
     * @param string $type
     * @return Forum_Message Provides a fluent interface
     */
    public function add($message, $type)
    {
        if (!isset($_SESSION[self::$_namespace][$type])) {
            $_SESSION[self::$_namespace][$type] = array();
        }
        $_SESSION[self::$_namespace][$type][] = $message;
        return $this;
    }
    
    /**
     * Add success message to current namespace
     * 
     * @param string $message
     * @return Forum_Message Provides a fluent interface
     */
    public function addSuccess($message)
    {
        $this->add($message, 'success');
        return $this;
    }
    
    /**
     * Add notice message to current namespace
     * 
     * @param string $message
     * @return Forum_Message Provides a fluent interface
     */
    public function addNotice($message)
    {
        $this->add($message, 'notice');
        return $this;
    }
    
    /**
     * Add warning message to current namespace
     * 
     * @param string $message
     * @return Forum_Message Provides a fluent interface
     */
    public function addWarning($message)
    {
        $this->add($message, 'warning');
        return $this;
    }
    
    /**
     * Add error message to current namespace
     * 
     * @param string $message
     * @return Forum_Message Provides a fluent interface
     */
    public function addError($message)
    {
        $this->add($message, 'error');
        return $this;
    }
    
    /**
     * Clears current message namespace
     * 
     * @return Forum_Message Provides a fluent interface
     */
    public function clear()
    {
        self::$_session->{self::$_namespace} = array();
        return $this;
    }
    
    /**
     * Retrieve array of messages from surrent namespace
     * 
     * @return array
     */
    public function get()
    {       
        $messages = array();
        
        if (isset($_SESSION[self::$_namespace])) {
            $messages = $_SESSION[self::$_namespace];
            unset($_SESSION[self::$_namespace]);
        }
        
        return $messages;
    }
}